package com.kfm.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/http")
public class MyHttpServlet extends HttpServlet {

    // Servlet 被容器调用的是 service 方法
    // 当你需要对于post、get请求分别做不同的处理的时候
    //
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("浏览器发送的是get请求");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("浏览器发送的是post请求");
    }



    // request 封装请求里面的所有东西 （请求头请求行请求体）
    // response 封装响应的内容
    // 最终tomcat调用的是Servlet的service方法 HttpServlet里面的service对提交的method做了判断调用不同的方法（doGet/doPost）
    // 重写的是 Servlet的方法 参数列表是 ServletRequest 和 ServletResponse
//    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
//        HttpServletRequest req = (HttpServletRequest) request;
//        HttpServletResponse resp = (HttpServletResponse) response;
//        // 获取本次请求提交的方式
//        String method = req.getMethod();
//        if ("get".equals(method)) {
//            doGet(req, resp);
//        } else if ("post".equals(method)) {
//            doPost(req, resp);
//        }
//    }

    // 这个重写的是HttpServlet类的方法 参数列表式 HttpServletRequest 和 HttpServletResponse
    // Tomcat service(ServletRequest, ServletResponse)
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(req.getMethod());
    }
}
